home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / umich / falcon / programm.ing / nt_dsp1.lzh / NT_DSP1.MSA / SORT / SORT2.ASM < prev    next >
Assembly Source File  |  1989-01-24  |  2KB  |  75 lines

  1. ;
  2. ; This program originally available on the Motorola DSP bulletin board.
  3. ; It is provided under a DISCLAIMER OF WARRANTY available from
  4. ; Motorola DSP Operation, 6501 Wm. Cannon Drive W., Austin, Tx., 78735.
  5. ; Last Update 24 Sep 87   Version 1.1 
  6. ;
  7. ; Sorting by Heapsort method
  8. ;
  9. sort2   macro   ARRAY,N_ITEMS   
  10. sort2   ident   1,1
  11.  
  12. ;
  13. ; reference: Niklaus Writh, "Algorithms + Data Structures = Programs", 
  14. ;            Prentice-Hall, 1976, Program 2.8, page 75
  15. ;
  16. ; ARRAY = location of an array of numbers in X memory space, 
  17. ;         first item is located at ARRAY.
  18. ; N_ITEMS = number of items in the array
  19. ;
  20. ; registers used: r0..r4
  21. ;                 a,b,y1,x0,x1
  22. ;                 assumes m0..m4 = $ffff
  23. ;                 uses 3 words of stack 
  24. ;
  25. ;
  26.         move    #ARRAY-1,b                      ; constant
  27.         move    #>(N_ITEMS/2)+ARRAY,r0          ;r0=l
  28.         move    #>ARRAY+N_ITEMS-1,r1            ;r1=r
  29.         move    r1,y1
  30.  
  31.         do      #N_ITEMS/2,_wend1
  32.         move                    (r0)-
  33.         jsr     _sift          
  34. _wend1
  35.  
  36.  
  37.         do      #N_ITEMS-1,_wend2
  38.         move    x:ARRAY,x1                      ;x1=x
  39.         move    x:(r1),x0
  40.         move    x0,x:ARRAY
  41.         move    x1,x:(r1)- 
  42.         move    r1,y1   
  43.         jsr     _sift
  44. _wend2  jmp     _end
  45.  
  46. ;
  47. ; sift
  48. _sift   move    r0,r3                           ;"i"=l
  49.         move    x:(r0),x1                       ;x=a("i")
  50. _loop3  move    r3,a                            ;a="i"
  51.         subl    b,a     r3,r2                   ;a = 2*i-array, i="i"=j
  52.         move    a,r3
  53.         cmp     y1,a    r3,r4                   ;j-r
  54.         jgt     _wend3
  55. ;if j<r
  56.         move    x:(r3)+,x0                      ;a(j)
  57.         jeq     _t1                             ;if j<r
  58. ;if a(j)<a(j+1)
  59.         move    x:(r3),a                        ;a(j+1)
  60.         cmp     x0,a                            ;a(j)-a(j+1)
  61. _t1     tle     x0,a    r4,r3
  62.         cmp     x1,a                            ;a(j)-x
  63.         jle     _t13
  64.         move    a,x:(r2)                        ;a(i)=a(j)
  65.         jmp     _loop3
  66. _wend3
  67. _t13    move    x1,x:(r2)                       ;a(i)=x
  68.         rts
  69. _end
  70.         endm
  71.  
  72.  
  73.